package com.myeasyexcel.utils;

/**
 * @author wangXiaoHu
 * @date 2021/12/14 16:23
 **/

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
 * 自定义excel头部样式拦截器
 *
 * @author wxh
 * @desc
 * @datetime 2020/9/30 14:33
 */

public class StyleExcelHandler implements SheetWriteHandler {
    /**
     * 指定注解的个数
     */
    static int annotationSize = 0;

    private static int getAnnotationSize(Class clazz,Class annotationClass){
        if(clazz == Object.class){
            return annotationSize;
        }
        Field[] declaredFields = clazz.getDeclaredFields();
        for(Field field:declaredFields){
            Annotation annotation = field.getAnnotation(annotationClass);
            if(annotation==null){
                continue;
            }
            Class<? extends Annotation> aClass = annotation.annotationType();
            if(aClass== annotationClass){
                annotationSize++;
            }
        }
        // 递归获取父类指定注解字段
        getAnnotationSize(clazz.getSuperclass(),annotationClass);
        return annotationSize;
    }

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
    }

    /**
     * 设置头部样式
     * @param writeWorkbookHolder
     * @param writeSheetHolder
     */
    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Class clazz = writeSheetHolder.getClazz();
        ExcelHeadContent annotation = (ExcelHeadContent) clazz.getAnnotation(ExcelHeadContent.class);
        int annotationSize = getAnnotationSize(clazz,ExcelProperty.class);

        Workbook workbook = writeWorkbookHolder.getWorkbook();
        Sheet sheet = workbook.getSheetAt(0);



        /**设置单元格格式为文本格式*/

        // 设置所有单元格水平垂直居中

        CellStyle allCellStyle = workbook.createCellStyle();
        allCellStyle.setAlignment(HorizontalAlignment.CENTER);// 水平居中
        allCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中

        // 设置单元格 格式为文本
        DataFormat fmt = workbook.createDataFormat();
        allCellStyle.setDataFormat(fmt.getFormat("@"));

        for(int i = 0;i< 20;i++){
            sheet.setDefaultColumnStyle(i, allCellStyle);
        }


        // 合并单元格
        CellRangeAddress cra = new CellRangeAddress(0, 0, 0, annotationSize); // 起始行, 终止行, 起始列, 终止列
        sheet.addMergedRegion(cra);
        // 设置标题
        Row row1 = sheet.createRow(0);// 行
        row1.setHeight((short) 4000);

        Cell cell1 = row1.createCell(0);// 列
        // 标题内容（来自注解）
        if(annotation != null){
            cell1.setCellValue(annotation.value());
        }
        CellStyle cellStyle = workbook.createCellStyle();
        // 文字颜色
        Font font = workbook.createFont();
        font.setColor(IndexedColors.BLACK.getIndex());

        cellStyle.setFont(font);
        // 根据手动\n换行
        cellStyle.setVerticalAlignment(VerticalAlignment.DISTRIBUTED);
        cell1.setCellStyle(cellStyle);
    }
}
